n=int(input())
n0=n
count=0
while n0 != 0:
    count+=1
    n0//10
if count != 4:
    print(0)
else:
    n1=n//1000
    n2=n//100%10
    n3=n//10%10
    n4=n%10
    maxPrime=0
    maybePrime=n1
    isPrime=1
    if maybePrime != 2:
        for i in range(2, maybePrime):
            if (maybePrime == 1 or maybePrime == 0 or maybePrime % i == 0):
                isPrime=0
    if isPrime == 1:
        if maybePrime > maxPrime:
            maxPrime = maybePrime
    maybePrime=n2
    isPrime=1
    if maybePrime != 2:
        for i in range(2, maybePrime):
            if (maybePrime == 1 or maybePrime == 0 or maybePrime % i == 0):
                isPrime=0
    if isPrime == 1:
        if maybePrime > maxPrime:
            maxPrime = maybePrime
    maybePrime=n3
    isPrime=1
    if maybePrime != 2:
        for i in range(2, maybePrime):
            if (maybePrime == 1 or maybePrime == 0 or maybePrime % i == 0):
                isPrime=0
    if isPrime == 1:
        if maybePrime > maxPrime:
            maxPrime = maybePrime
    maybePrime=n4
    isPrime=1
    if maybePrime != 2:
        for i in range(2, maybePrime):
            if (maybePrime == 1 or maybePrime == 0 or maybePrime % i == 0):
                isPrime=0
    if isPrime == 1:
        if maybePrime > maxPrime:
            maxPrime = maybePrime
    maybePrime=n1*10+n2
    isPrime=1
    if maybePrime != 2:
        for i in range(2, maybePrime):
            if (maybePrime == 1 or maybePrime == 0 or maybePrime % i == 0):
                isPrime=0
    if isPrime == 1:
        if maybePrime > maxPrime:
            maxPrime = maybePrime
    maybePrime=n1*100+n2*10+n3
    isPrime=1
    if maybePrime != 2:
        for i in range(2, maybePrime):
            if (maybePrime == 1 or maybePrime == 0 or maybePrime % i == 0):
                isPrime=0
    if isPrime == 1:
        if maybePrime > maxPrime:
            maxPrime = maybePrime
    maybePrime=n1*1000+n2*100+n3*10+n4
    isPrime=1
    if maybePrime != 2:
        for i in range(2, maybePrime):
            if (maybePrime == 1 or maybePrime == 0 or maybePrime % i == 0):
                isPrime=0
    if isPrime == 1:
        if maybePrime > maxPrime:
            maxPrime = maybePrime
    maybePrime=n1*10+n3
    isPrime=1
    if maybePrime != 2:
        for i in range(2, maybePrime):
            if (maybePrime == 1 or maybePrime == 0 or maybePrime % i == 0):
                isPrime=0
    if isPrime == 1:
        if maybePrime > maxPrime:
            maxPrime = maybePrime
    maybePrime=n1*100+n3*10+n4
    isPrime=1
    if maybePrime != 2:
        for i in range(2, maybePrime):
            if (maybePrime == 1 or maybePrime == 0 or maybePrime % i == 0):
                isPrime=0
    if isPrime == 1:
        if maybePrime > maxPrime:
            maxPrime = maybePrime
    maybePrime=n1*10+n4
    isPrime=1
    if maybePrime != 2:
        for i in range(2, maybePrime):
            if (maybePrime == 1 or maybePrime == 0 or maybePrime % i == 0):
                isPrime=0
    if isPrime == 1:
        if maybePrime > maxPrime:
            maxPrime = maybePrime
    maybePrime=n2*10+n3
    isPrime=1
    if maybePrime != 2:
        for i in range(2, maybePrime):
            if (maybePrime == 1 or maybePrime == 0 or maybePrime % i == 0):
                isPrime=0
    if isPrime == 1:
        if maybePrime > maxPrime:
            maxPrime = maybePrime
    maybePrime=n2*100+n3*10+n4
    isPrime=1
    if maybePrime != 2:
        for i in range(2, maybePrime):
            if (maybePrime == 1 or maybePrime == 0 or maybePrime % i == 0):
                isPrime=0
    if isPrime == 1:
        if maybePrime > maxPrime:
            maxPrime = maybePrime
    maybePrime=n2*10+n4
    isPrime=1
    if maybePrime != 2:
        for i in range(2, maybePrime):
            if (maybePrime == 1 or maybePrime == 0 or maybePrime % i == 0):
                isPrime=0
    if isPrime == 1:
        if maybePrime > maxPrime:
            maxPrime = maybePrime
    maybePrime=n3*10+n4
    isPrime=1
    if maybePrime != 2:
        for i in range(2, maybePrime):
            if (maybePrime == 1 or maybePrime == 0 or maybePrime % i == 0):
                isPrime=0
    if isPrime == 1:
        if maybePrime > maxPrime:
            maxPrime = maybePrime
    print(maxPrime)
    
    
